package com.fengwk.resource.controller;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.TransportException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fengwk.resource.service.UploadService;
import com.fengwk.support.util.ResultUtils;
import com.fengwk.user.face.annotation.Logined;
import com.fengwk.user.face.annotation.UserId;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "上传")
@RestController
@RequestMapping("/api/upload")
public class UploadController {

	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpServletResponse response;
	@Autowired
	private UploadService uploadService;
	
	@Logined
	@ApiOperation(value = "files上传")
	@PostMapping("/files")
	public void files(
			@UserId Long userId, 
			HttpServletRequest request) throws IOException, InvalidRemoteException, TransportException, GitAPIException, InterruptedException {
		ResultUtils.returnJsonResult(request, response, uploadService.files(userId, request));
	}
	
	@Logined
	@ApiOperation(value = "urls上传")
	@PostMapping("/urls")
	public void urls(
			@UserId Long userId,
			@RequestParam("urls") Collection<String> urls) throws MalformedURLException {
		ResultUtils.returnJsonResult(request, response, uploadService.urls(userId, urls));
	}
	
}
